$(function () {

  // 模拟点击
  $('.btn-sm').on('click', function () {
    $('#flie_e').click()
  })
  // 获取图片
  $('#flie_e').on('change', function (e) {
    let files = e.target.files
    if (files.length === 0) {
      return
    }
    var newImgURL = URL.createObjectURL(files[0])
    $('#image').attr('src', newImgURL)

  });

  // 分类标签
  getAlladd()
  function getAlladd () {
    $.ajax({
      method: 'GET',
      url: '/index/index',
      success: function (res) {
        let htmlSte = template('tpl_y', res.data)
        $('#from_v').html(htmlSte)

        let htmlStee = template('tpl_M', res.data)
        $('#from_V').html(htmlStee)
      }
    })
  }

  // 导航头渲染
  getAllinit()
  function getAllinit () {
    $.ajax({
      method: 'GET',
      url: '/index/index',
      success: function (res) {
        if (res.errno) {
          return alert('请求数据失败')
        }
        let htmlSet = template('tpl', res.data)
        $('#tpl_sj').html(htmlSet)
      }
    })
  }


  // 发布请求
  $('#btn_fb').on('click', function () {
    $.ajax({
      method: 'POST',
      url: '/user/addArticle',
      data: {
        status: $('[name=status]').val(),
        author: $('[name=author]').val(),
        content: $('[name=content]').val(),
        title: $('[name=title]').val(),
        tags: $('[name=tags]').val(),
        cateid: $('[name=cateid]').val(),
        pic: $('[name=pic]').attr('src'),
      },
      success: function (res) {
        if (res.errno) {
          return alert('发布文章失败')
        }
        alert('发布文章成功')      
        location.href = '/web-static/user-list.html'
      }
    })
  })

// 存为草稿
  $('#btn_cg').on('click', function () {
    $.ajax({
      method: 'POST',
      url: '/user/addArticle',
      data: {
        status: $('[name=status]').val(),
        author: $('[name=author]').val(),
        content: $('[name=content]').val(),
        title: $('[name=title]').val(),
        tags: $('[name=tags]').val(),
        cateid: $('[name=cateid]').val(),
        pic: $('[name=pic]').attr('src'),
      },
      success: function (res) {
        if (res.errno) {
          return alert('存稿失败')
        }
        alert('以存为草稿')
        location.href = '/web-static/user-list.html'
      }
    })
  })
})
